package linked.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import linked.bean.Actor;
import linked.bean.Movie;

import TRIR.XMLParser.Predstava;
import TRIR.XMLParser.XmlParser;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Resource;
import com.sun.jersey.api.NotFoundException;

@Path("/movie")
public class MovieResource {
	
	@Path("{name}")
	@GET
	@Produces({MediaType.APPLICATION_XML,MediaType.TEXT_PLAIN})
	public Movie getMovieByName(@PathParam("name") String name){
		
		
		String actorQuery = 
				"PREFIX m: <http://data.linkedmdb.org/resource/movie/>"+
				"PREFIX f: <http://xmlns.com/foaf/0.1/>"+
				"PREFIX dc: <http://purl.org/dc/terms/>"+
				"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
				"SELECT ?actorName ?actorId ?movieId ?release ?genreName ?runtime"+
				" WHERE {" +
					"?film rdfs:label '"+name+"'."+
					"?film m:actor ?actor."+
					"?film m:filmid ?movieId."+
					"?actor m:actor_name ?actorName."+
					"?actor m:actor_actorid ?actorId."+
					"?film m:initial_release_date ?release."+
					"OPTIONAL{?film m:genre ?genre. " +
					"?genre m:film_genre_name ?genreName." +
					"?film m:runtime ?runtime}"+
				"}";
		
		
		Query actorQ = QueryFactory.create(actorQuery);
		QueryExecution qe = QueryExecutionFactory.sparqlService("http://data.linkedmdb.org/sparql", actorQ);
		ResultSet results = null;
		try {
			results = qe.execSelect();
		} catch (Exception e) {
			e.getMessage();
		}
		
		
		Movie m = new Movie();
		
		List<Actor> actorList = new ArrayList<Actor>();
		
		while(results.hasNext()){
			QuerySolution qs = results.next();
			Literal actorName = qs.getLiteral("actorName");
			Literal actorId = qs.getLiteral("actorId");
			m.setId(qs.getLiteral("movieId").getInt());
			
			if (qs.getLiteral("release") == null) {
				m.setReleaseDate("N/A");
			} else {
				m.setReleaseDate(qs.getLiteral("release").getString());
			}
			
			if(qs.getLiteral("genreName") == null){
				m.setGenre("N/A");
			}else{
				m.setGenre(qs.getLiteral("genreName").getString());
			}
			
			if(qs.getLiteral("runtime") == null){
				m.setRuntime("N/A");
			}else{
				m.setRuntime(qs.getLiteral("runtime").getString());
			}
			
			if(!actorName.getString().isEmpty()){
				Actor a = new Actor();
				a.setName(actorName.getString());
				a.setId(actorId.getInt());
				actorList.add(a);
			}else{
				throw new NotFoundException("actor without a name???");
			}
			
			
		}
		
		qe.close();
		m.setActors(actorList);
		//m.setId(77);
		//m.setGenre("Drama");
		m.setLabel(name);
		
		
		return m;
	}
	
	@GET
	@Path("/resource/{searchstring}")
	@Produces({MediaType.APPLICATION_XML,MediaType.TEXT_PLAIN})
	public String getMovieResources(@PathParam("searchstring") String searchString){
		
		String searchQuery = 
				"PREFIX m: <http://data.linkedmdb.org/resource/movie/>"+
				"PREFIX f: <http://xmlns.com/foaf/0.1/>"+
				"PREFIX dc: <http://purl.org/dc/terms/>"+
				"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
				"SELECT ?resource"+
				" WHERE {" +
					"?resource rdfs:label '"+searchString+"'."+					
				"}";
		
		Query q = QueryFactory.create(searchQuery);		
		QueryExecution qe = QueryExecutionFactory.sparqlService("http://data.linkedmdb.org/sparql", q);		
		ResultSet results = qe.execSelect();
		return ResultSetFormatter.asXMLString(results);
	}
	
	
	public String getMovieById(String id){
		String searchQuery = 
				"PREFIX m: <http://data.linkedmdb.org/resource/movie/>"+
				"PREFIX f: <http://xmlns.com/foaf/0.1/>"+
				"PREFIX dc: <http://purl.org/dc/terms/>"+
				"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
				"SELECT ?resource"+
				" WHERE {" +
					"<http://data.linkedmdb.org/resource/film/"+id+">a ?value;"+
					" m:actor ?actor."+
					" m:filmid ?movieId."+
					"?actor m:actor_name ?actorName."+
					"?actor m:actor_actorid ?actorId."+
					"?film m:initial_release_date ?release."+
					"OPTIONAL{?film m:genre ?genre. " +
					"?genre m:film_genre_name ?genreName." +
					"?film m:runtime ?runtime}"+
				"}";
		
		Query q = QueryFactory.create(searchQuery);
		
		//QueryExecution qe = QueryExecutionFactory.create(q);
		QueryExecution qe = QueryExecutionFactory.sparqlService("http://data.linkedmdb.org/sparql", q);
		
		ResultSet results = qe.execSelect();
		ResultSetFormatter.out(System.out, results);
		return "";
	}
	
	
	@GET
	@Path("/actor/{name}")
	@Produces(MediaType.APPLICATION_XML)
	public Actor getActorMovies(@PathParam("name") String actorName){
		
		
		String searchQuery = 
				"PREFIX m: <http://data.linkedmdb.org/resource/movie/>"+
				"PREFIX f: <http://xmlns.com/foaf/0.1/>"+
				"PREFIX dc: <http://purl.org/dc/terms/>"+
				"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
				"SELECT ?movieName ?date"+
				" WHERE {" +
					"?actor m:actor_name '"+actorName+"'." +
					"?movie m:actor ?actor." +
					"?movie rdfs:label ?movieName." +
					"?movie m:initial_release_date ?date."+					
				"}";
		
		Query q = QueryFactory.create(searchQuery);
		
		//QueryExecution qe = QueryExecutionFactory.create(q);
		QueryExecution qe = QueryExecutionFactory.sparqlService("http://data.linkedmdb.org/sparql", q);
		
		ResultSet results = qe.execSelect();
		//ResultSetFormatter.out(System.out, results);
		
		Actor a = new Actor();
		
		List<Movie> movieList = new ArrayList<Movie>();
		
		while(results.hasNext()){
			Movie m = new Movie();
			QuerySolution qs = results.next();
			Literal movieName = qs.getLiteral("movieName");
			
			Literal movieYear = qs.getLiteral("date");
			m.setLabel(movieName.getString());
			m.setReleaseDate(movieYear.getString());
			movieList.add(m);
		}
		
		a.setMovies(movieList);
		a.setName(actorName);
		return a;
	}
	
	@GET
	@Path("/shows/{cityS}-{kinoS}")
	@Produces({MediaType.APPLICATION_XML,MediaType.TEXT_PLAIN})
	public ArrayList<Predstava> getMoviesShows(@PathParam("cityS") String city, @PathParam("kinoS") String kino){

	XmlParser x=new XmlParser();
	return x.getAllShows(city, kino);


	}
}
